package com.zzyl.nursing.controller.memberConteoller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocalUtil;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.member.MemberReservationService;
import com.zzyl.nursing.vo.memberVo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private MemberReservationService memberReservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long id = UserThreadLocalUtil.getUserId();
        int count = memberReservationService.getUserCancelledCount(id);
        return R.ok(count);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询指定时间段预约数量")
    public R<List<TimeCountVo>> getCountByTime() {
        List<TimeCountVo> countVoList = memberReservationService.getCountByTime();
        return R.ok(countVoList);
    }

    @PostMapping
    @ApiOperation("新增预约")
    public AjaxResult addReservation(@RequestBody Reservation reservation) {

        log.info("/member/reservation 新增预约: {}", reservation);
        reservation.setCreateBy(String.valueOf(UserThreadLocalUtil.getUserId()));
        memberReservationService.save( reservation);
        return AjaxResult.success("新增成功");
    }

    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public AjaxResult page() {
        startPage();
        List<Reservation> list = memberReservationService.getPageList();
        TableDataInfo data = getDataTable(list);
        return success(data);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询预约信息")
    public AjaxResult list() {
        startPage();
        List<Reservation> list = memberReservationService.getPageList();
        TableDataInfo data = getDataTable(list);
        return success(data);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancelReservation(@PathVariable Long id) {
        log.info("/member/reservation/{} 取消预约", id);
        memberReservationService.updateReservationById(id);
        return AjaxResult.success("取消成功");
    }

}